package com.bos.controller;

import com.bos.controller.base.BaseController;
import com.bos.controller.base.imp.BaseControllerImp;
import com.bos.domain.Staff;
import com.bos.service.IStaffService;
import com.bos.utils.BOSUtil;
import com.bos.utils.PageBean;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Administrator
 */
@Controller
public class StaffController extends BaseControllerImp<Staff> implements BaseController<Staff> {

    @Autowired
    private IStaffService staffService;

    private final String LIST="page/base/staff";

    @Autowired
    public StaffController(PageBean pageBean) {
        super(pageBean);
    }

    @RequestMapping(value = "/addStaff",method = RequestMethod.POST)
    public String AddStaff(Staff bcStaff){

        staffService.addStaff(bcStaff);
        //使用重定向方式以免当重新载入页面时数据再次提交
        return "redirect:"+LIST;
    }

    @RequestMapping(value = "/staffPageQuery",method = RequestMethod.GET)
    @ResponseBody
    public String pageQuery(int page ,int rows){
        //将当前页号和页面大小至于session域中，以便刷新时保持不变
        BOSUtil.getSession().setAttribute("staffpage",page);
        BOSUtil.getSession().setAttribute("staffrows",rows);
        setPageAndRows(page,rows);
        staffService.pageQuery(super.pageBean);
        return java2json(new String[]{"currentPage","pageSize","detachedCriteria","decidedzones"});
    }

    /*方法权限控制，要求改用户拥有delete-staff权限*/
    @RequiresPermissions("delete-staff")
    @RequestMapping(value = "/invalidStaff",method = RequestMethod.POST)
    @ResponseBody
    public String deleteBatch(@RequestBody String  ids)  {
        boolean success=true;
        try {
            staffService.deleteBatch(ids);
        }catch (Exception e){
            e.printStackTrace();
            success=false;
        }
        return String.valueOf(success);
    }



    @RequestMapping(value = "/restoreStaff",method = RequestMethod.POST)
    @ResponseBody
    public String restoreBatch(@RequestBody String  ids)  {
        boolean success=true;
        try {
            staffService.restoreBatch(ids);
        }catch (Exception e){
            e.printStackTrace();
            success=false;
        }
        return String.valueOf(success);
    }

    @RequestMapping(value = "/editStaff",method = RequestMethod.POST)
    public String editStaff(Staff staff)  {
        staffService.editStaff(staff);
        return "redirect:"+LIST;
    }




}
